﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace NetMicro.EventBus.Extensions
{
    /// <summary>
    /// 跟踪扩展
    /// </summary>
    public static class TraceContext
    {
        /// <summary>
        /// 导入跟踪id
        /// </summary>
        /// <param name="props"></param>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public static void InjectTraceContextIntoBasicProperties(IDictionary<string, string> props, string key, string value)
        {
            try
            {
                props[key] = value;
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 导出跟踪id
        /// </summary>
        /// <param name="props"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static IEnumerable<string> ExtractTraceContextFromBasicProperties(IDictionary<string, string> props, string key)
        {
            try
            {
                if (props.TryGetValue(key, out var value))
                {

                    List<string> list = new();
                    list.Add(value.ToString());
                    return list.ToArray();
                }
            }
            catch (Exception ex)
            {

            }

            return Enumerable.Empty<string>();
        }
    }
}
